﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;

namespace MVVMComun
{
    public class WeatherService:ServiceModel
    {
        private string key = "3a03267917e14923";

        private string url ="http://api.wunderground.com/api/{0}/conditions/forecast/lang:SP/q/{1}/{2}.xml";

        private string pais;

        public event EventHandler<WeatherServiceResponseEventArgs> WeatherResponseReady;


        public string Pais
        {
            get { return pais; }
            set { pais = value; }
        }

        private string ciudad;

        public string Ciudad
        {
            get { return ciudad; }
            set { ciudad = value; }
        }
    
        public WeatherService() {
        
        }


        public async void getForeCast() {
            try { 
            serviceUri = new Uri(string.Format(url, key, Pais, ciudad));
            List<Pronostico> pronosticos = new List<Pronostico>();
            HttpClient hc = new HttpClient();
            var result = await hc.GetStringAsync(serviceUri);
            XDocument doc = XDocument.Parse(result);
            var actualTime = (from day in doc.Descendants("current_observation")
                              select new Pronostico
                              {
                                  TempC = day.Element("temp_c").Value,
                                  TempF = day.Element("temp_f").Value,
                                  Condicion = day.Element("weather").Value,
                                  Icon = day.Element("icon").Value,
                                  IconUrl = day.Element("icon_url").Value

                              }).ToList();

        
            var days = (from day in doc.Descendants("simpleforecast").Descendants("forecastday")
                        select day).ToList();



            var resultForeCast = (from aux in days
                          select new Pronostico
                          {
                              Dia = aux.Descendants("weekday").First().Value,
                              AltaC = aux.Descendants("high").First().Element("celsius").Value,
                              AltaF = aux.Descendants("high").First().Element("fahrenheit").Value,
                              BajaC = aux.Descendants("low").First().Element("celsius").Value,
                              BajaF = aux.Descendants("low").First().Element("fahrenheit").Value,
                              Condicion = aux.Descendants("conditions").First().Value,
                              Icon = aux.Descendants("icon").First().Value,
                              IconUrl = aux.Descendants("icon_url").First().Value
                          }).ToList();

            pronosticos.AddRange(resultForeCast);

            pronosticos[0].TempC = actualTime[0].TempC;
            pronosticos[0].TempF = actualTime[0].TempF;
            pronosticos[0].TempC = actualTime[0].TempC;

            if (WeatherResponseReady != null)
                  WeatherResponseReady(this, new WeatherServiceResponseEventArgs(pronosticos));
            }
            catch (Exception e) {

                if (WeatherResponseReady != null)
                    WeatherResponseReady(this, new WeatherServiceResponseEventArgs(new List<Pronostico>() {
                        new Pronostico(){ Dia="Sin Informacion.",Condicion="Sin Informacion" } }));
            
            }
        }


    
    }
}
